package com.obas.component.tree {
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.component.tree.treeclass.Node;
   import com.obas.constants.TestingLibConstants;
   import com.obas.constants.StringConstants;
   import com.obas.event.ItestOperationEvent;
   import com.obas.flex.common.util.ResourceUtil;
   import com.obas.model.business.BusinessFunction;
   import com.obas.model.business.BusinessRepository;
   import com.obas.model.business.BusinessScenario;
   import com.obas.model.business.BusinessUsecase;
   import com.obas.view.menu.BusinessCaseMenu;
   import com.obas.view.menu.BusinessFunctionMenu;
   import com.obas.view.menu.BusinessScenarioMenu;
   import com.obas.view.menu.RepositoryMenu;
   import com.ease.component.controls.Menu;

   import mx.collections.ArrayCollection;
   import mx.core.Container;

   [Event(name="{com.obas.event.ItestOperationEvent.REQUEST_ID}", type="com.obas.event.ItestOperationEvent")]
   public class BusinessTree extends LoadingTree {
      public function BusinessTree() {

      }

      override protected function buildMenu(container:Container):void {
         var repositoryMenu:Menu = new RepositoryMenu();
         container.addChild(repositoryMenu);
         _menuMap[TestingLibConstants.ITEMTYPE_BUSINESSREPOSITORY] = repositoryMenu;

         var scenarioMenu:Menu = new BusinessScenarioMenu();
         container.addChild(scenarioMenu);
         _menuMap[TestingLibConstants.ITEMTYPE_BUSINESSSCENARIO] = scenarioMenu;

         var caseMenu:Menu = new BusinessCaseMenu();
         container.addChild(caseMenu);
         _menuMap[TestingLibConstants.ITEMTYPE_BUSINESSUSECASE] = caseMenu;

         var functionMenu:Menu = new BusinessFunctionMenu();
         container.addChild(functionMenu);
         _menuMap[TestingLibConstants.ITEMTYPE_BUSINESSFUNCTION] = functionMenu;

      }

      //点击展开节点，获取节点下的node
      override protected function callRemoteService(node:Node):void {
         var entity:* = node.entity;
         if (entity is BusinessRepository) {
            dispatchEvent(new ItestOperationEvent([entity.id, node], ItestOperationEvent.GET_BUSINESSSCENARIO));
         }
         else if (entity is BusinessScenario) {
            dispatchEvent(new ItestOperationEvent([entity.id, node], ItestOperationEvent.GET_BUSINESSUSECASE));
         }
         else if (entity is BusinessUsecase) {
            dispatchEvent(new ItestOperationEvent([entity.id, node], ItestOperationEvent.GET_BUSINESSFUNCTION));
         }
      }

      [ResponseHandler("{com.obas.event.ItestOperationEvent.RETURN_ID}")]
      public function onItestOperationEvent(event:CommandCompletionEvent):void {
         var loadingTreeEvent:ItestOperationEvent = event.request as ItestOperationEvent;
         var requestNode:Node = loadingTreeEvent.spec[1];

         if (event.error) {
            const errorMsg:String = ResourceUtil.getErrorString(StringConstants.EXPLORER_ERROR, event.error);
            addErrorStat(requestNode, errorMsg);
            return ;
         }

         var result:ArrayCollection = event.result as ArrayCollection;
         removeNodeStat(requestNode);
         var childNode:Node = null;
         for each (var childEntity:Object in result) {
            childNode = nodeFactory.createNode(childEntity);
            childNode.parentNode = requestNode;
            childNode.type = getNodeType(childEntity);
            if (childNode.type == TestingLibConstants.ITEMTYPE_BUSINESSFUNCTION) {
               childNode.children = null;
            }
            requestNode.children.addItem(childNode);
         }

         requestNode.initialized = true;
         expandItem(requestNode, false);
         expandItem(requestNode, true);
      }

      public function getNodeType(item:*):String {
         if (item is BusinessRepository) {
            return TestingLibConstants.ITEMTYPE_BUSINESSREPOSITORY;
         }
         else if (item is BusinessScenario) {
            return TestingLibConstants.ITEMTYPE_BUSINESSSCENARIO;
         }
         else if (item is BusinessUsecase) {
            return TestingLibConstants.ITEMTYPE_BUSINESSUSECASE;
         }
         else if (item is BusinessFunction) {
            return TestingLibConstants.ITEMTYPE_BUSINESSFUNCTION;
         }
         return "";
      }
   }
}